<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/common/inc::header"/>
<body>
<form class="layui-form">
    <input name="id" id="id" type="hidden"/>
    <input name="dictImg" id="dictImg" type="hidden"/>
    <br/>
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label">标签大类</label>
        <div class="layui-input-inline">
            <select name="dictEntry" id="dictEntry" lay-verify="required">
                <option value="basic_equipment">基础设备</option>
                <option value="bathroom_equipment">卫浴设备</option>
                <option value="household_equipment">家用电器</option>
                <option value="general_equipment">通用设施</option>
                <option value="service_equipment">服务项目</option>
            </select>
        </div>
    </div>
    <hr class="layui-bg-gray">
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label">标签图片:</label>
        <div class="layui-input-inline">
            <div class="image-uploader"  style="width: 100px">
                <div class="image-preview" style="width: 100px;height: 100px">
                    <div class="image">
                        <img src="" id="img"  style="width: 100px;height: 100px">
                        <span class="image-browse"  style="width: 100px" onclick="uploadFun('img')"><strong>上传图片</strong><span  style="width: 100px"
                                class="progress"><span class="progress-bar"></span></span></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-input-inline" style="margin-left: -60px">
            建议尺寸 100X100
        </div>
    </div>
    <hr class="layui-bg-gray">
    <div class="layui-form-item" style="margin-left: 20%">
        <label class="layui-form-label">标签内容</label>
        <div class="layui-input-inline">
            <input type="text" name="dictPrompt" id="dictPrompt" autocomplete="off" required
                   class="layui-input">
        </div>
    </div>
    <button class="layui-btn" id="formSubmit" lay-submit="" lay-filter="formSubmit" style="display: none">立即提交</button>
</form>
<script th:replace="/common/inc::incJs"></script>
<script>
    var form = layui.form;
    var successCallback;
    var id = getQueryString("id");
    $("#id").val(id);

    var config = {
        width: 100,
        height: 100,
        strictSize: true,
        url: "",
        back: "",
        obj: null,
        formobj: null,
        status: null,
        type:"single"
    };

    var uploadFun = function (obj) {
        config.obj = $("#" + obj);
        config.formobj = $(':input[name="dictImg"]');
        openDialog({
            url: "/backend/security/customer/uploadImage",
            area: ["50%", "65%"]
        });
    };

    //监听提交
    form.on('submit(formSubmit)', function (data) {
        if (id != "") {
            if ($("#dictEntry").val() == "Promotion") {
                data.field.subEntry = hex_md5($("#dictPrompt").val()).substr(8, 16);
            } else {
                data.field.subEntry = null;
            }
            $.post('/backend/security/sysDictionary/update', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                }
                else {
                    layer.msg('操作成功');
                    successCallback && successCallback();
                }
            });
        }
        else {
            if ($("#dictEntry").val() == "Promotion") {
                data.field.subEntry = hex_md5($("#dictPrompt").val()).substr(8, 16);
            }
            $.post('/backend/security/sysDictionary/add', data.field, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                }
                else {
                    layer.msg('操作成功');
                    successCallback && successCallback();
                }
            });
        }
        return false;
    });

    var formSubmit = function (callback) {
        successCallback = callback;
        $("#formSubmit").click();
    };


    $(function () {
        if (id != "") {
            $.post('/backend/security/sysDictionary/getById', {id: id}, function (res) {
                if (res.code < 0) {
                    layer.msg(res.message);
                }
                else {
                    $("form").loadData(res.body);
                    $("#dictImg").val(res.body.dictImg);
                    $("#img").attr("src",res.body.dictImg);
                    form.render('select');
                }
            });
        }
    })
</script>
</body>
</html>
